//
//  BaseNaviViewController.m
//  crm
//
//  Created by weacar on 2017/12/14.
//  Copyright © 2017年 weacar.com. All rights reserved.
//

#import "BaseNaviViewController.h"

@interface BaseNaviViewController () <UINavigationControllerDelegate,UIGestureRecognizerDelegate>

@end

@implementation BaseNaviViewController


+ (void)initialize
{
    //导航栏主题 title文字属性
    UINavigationBar *navBar = [UINavigationBar appearance];
    //导航栏背景图
    //iOS 7之后更改背景颜色
    [navBar setBarTintColor:[WWColorKit CCFFFFFF]];
    //iOS 7之前更改背景颜色
    [navBar setTintColor:[UIColor grayColor]];
    
    //修改标题大颜色和文字大小
    
    [navBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[WWColorKit CC252525], NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Light" size:18]}];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //遵守UINavigationControllerDelegate代理
    self.delegate = self;
    //遵守UIGestureRecognizerDelegate代理
    self.interactivePopGestureRecognizer.delegate = self;
    //默认开启返回收拾
    self.interactivePopGestureRecognizer.enabled = YES;
    
    //
    self.navigationBar.translucent = NO;
    
    //更改底部黑线颜色
    UIImageView *navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationBar];
    
    UIImageView *imgView = [[UIImageView alloc]init];
    
    [navBarHairlineImageView addSubview:imgView];
    
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(navBarHairlineImageView);
    }];
    imgView.backgroundColor = KColor_White;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ----------解决手势问题----------
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    //此处一定注意如果是第一个视图。需要屏蔽返回手势，不然会有BUG。
    if (viewController == self.viewControllers[0]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }else{
        self.interactivePopGestureRecognizer.enabled = YES;
    }
}

#pragma mark -
#pragma mark ----------push时隐藏TabBar----------
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if(self.viewControllers.count > 0){
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}

#pragma mark -
#pragma mark ----------返回到指定视图----------
- (void)popToAppointViewController:(NSString *)ClassName animated:(BOOL)animated
{
    id vc = [self getCurrentViewControllerClass:ClassName];
    if(vc != nil && [vc isKindOfClass:[UIViewController class]])
    {
        [self popToViewController:vc animated:animated];
    }else{
        NSLog(@"没有找到此控制器");
    }
}

- (instancetype)getCurrentViewControllerClass:(NSString *)ClassName
{
    Class classObj = NSClassFromString(ClassName);
    
    NSArray * szArray =  self.viewControllers;
    for (id vc in szArray) {
        if([vc isMemberOfClass:classObj])
        {
            return vc;
        }
    }
    return nil;
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}


@end
